package com.ruoyi.web.controller.search.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.web.controller.search.model.EsTextDocument;
import com.ruoyi.web.controller.search.model.Search;
import com.ruoyi.web.controller.search.service.ISearchService;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 测试管理[全文检索]
 *
 * @author ruoyi
 */
@Controller
@ConditionalOnProperty(prefix = "easy-es", name = "enable", havingValue = "true")
@RequestMapping("/search/fulltext")
public class TextSearchController {
    @Autowired
    private ISearchService searchService;

    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 查询全文检索列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Search search) {
        EsPageInfo<EsTextDocument> esPageInfo = searchService.selectTextList(search);
        long total = esPageInfo.getTotal();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(esPageInfo.getList());
        rspData.setTotal(total > 200 ? 200 : total);
        return rspData;
    }

    /**
     * 新增公告
     */
    @GetMapping("/add")
    public String add() {
        return "system/notice/add";
    }

    /**
     * 新增保存公告
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysNotice notice) {
        noticeService.insertNotice(notice);
        EsTextDocument esTextDocument = new EsTextDocument();
        BeanUtils.copyBeanProp(esTextDocument, notice);
        searchService.addEsTextDocument(esTextDocument);
        return AjaxResult.success();
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Long noticeId, ModelMap mmap) {
        EsTextDocument esTextDocument = searchService.getEsTextDocument(noticeId);
        SysNotice notice = new SysNotice();
        BeanUtils.copyBeanProp(notice, esTextDocument);
        mmap.put("notice", notice);
        return "system/notice/edit";
    }

    /**
     * 修改保存公告
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysNotice notice) {
        noticeService.updateNotice(notice);
        EsTextDocument esTextDocument = new EsTextDocument();
        BeanUtils.copyBeanProp(esTextDocument, notice);
        searchService.updateEsTextDocument(esTextDocument);
        return AjaxResult.success();
    }

    /**
     * 查询公告详细
     */
    @GetMapping("/view/{noticeId}")
    public String view(@PathVariable("noticeId") Long noticeId, ModelMap mmap) {
        EsTextDocument esTextDocument = searchService.getEsTextDocument(noticeId);
        SysNotice notice = new SysNotice();
        BeanUtils.copyBeanProp(notice, esTextDocument);
        mmap.put("notice", notice);
        return "system/notice/view";
    }

    /**
     * 删除公告
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        noticeService.deleteNoticeByIds(ids);
        searchService.deleteEsTextDocument(ids);
        return AjaxResult.success();
    }

    /**
     * 刷新全文检索数据
     */
    @GetMapping("/resetTextCache")
    @ResponseBody
    public AjaxResult resetTextCache() {
        searchService.resetTextCache();
        return AjaxResult.success();
    }
}
